<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" inline label-width="100px">
            <el-form-item :label="$t('shiJianXuanZe') + '：'" class="width100" style="display: block">
              <el-radio-group
                v-model="tableFrom.dateLimit"
                type="button"
                class="mr20"
                size="small"
                @change="selectChange(tableFrom.dateLimit)"
              >
                <el-radio-button v-for="(item, i) in fromList.fromTxt" :key="i" :label="item.val">{{
                  item.text
                }}</el-radio-button>
              </el-radio-group>
              <el-date-picker
                v-model="timeVal"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                size="small"
                type="daterange"
                placement="bottom-end"
                :start-placeholder="$t('kaiShiRiQi')"
                :end-placeholder="$t('jieShuRiQi')"
                style="width: 250px"
                @change="onchangeTime"
              />
            </el-form-item>
            <el-form-item :label="$t('shenHeZhuangTai') + '：'">
              <el-radio-group v-model="tableFrom.offlinePayStatus" type="button" @change="getList(1)">
                <el-radio-button label="">{{ $t('quanBu') }} ({{ chartType.all }})</el-radio-button>
                <el-radio-button label="need_pay">{{ $t('daiZhiFu') }} ({{ chartType.need_pay }})</el-radio-button>
                <el-radio-button label="doing">{{ $t('shenHeZhong') }} ({{ chartType.doing }})</el-radio-button>
                <el-radio-button label="pay_fail">{{ $t('zhiFuShiBai') }} ({{ chartType.pay_fail }})</el-radio-button>
                <el-radio-button label="has_pay">{{ $t('yiZhiFu') }} ({{ chartType.has_pay }})</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <br />
            <el-form-item :label="$t('zhuDingDanHao') + '：'" class="width100">
              <el-input
                v-model="tableFrom.masterOrderNo"
                @keyup.enter.native="getList(1)"
                :placeholder="$t('qingShuRuZhuDingDanHao')"
                class="selWidth"
                size="small"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  size="small"
                  class="el-button-solt"
                  @click="getList(1)"
                />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-table v-loading="listLoading" tooltip-effect="dark" :data="tableData.data" style="width: 100%" class="table">
        <el-table-column prop="id" label="ID" min-width="60" />
        <el-table-column prop="orderNo" :label="$t('zhuDingDanHao')" min-width="120" />
        <!-- <el-table-column prop="nickName" label="对方信息" min-width="100" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{ scope.row.nickName }} | {{ scope.row.uid }}</span>
          </template>
        </el-table-column> -->
        <el-table-column prop="payPrice" :label="$t('zhuanZhangJinE')" min-width="120" />
        <el-table-column :label="$t('zhuanZhangPingZheng')" min-width="160">
          <template slot-scope="scope">
            <div class="image__preview" v-if="scope.row.offlinePayFile">
              <el-image
                v-for="(item, index) in scope.row.offlinePayFile"
                :key="index"
                style="width: 100px; height: 100px"
                :src="item"
                :preview-src-list="scope.row.offlinePayFile"
              >
              </el-image>
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('shenHeZhuangTai')" min-width="120">
          <template slot-scope="scope">
            <span>{{ offlinePayStatus[scope.row.offlinePayStatus] || $t('weiZhiZhuangTai') }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="payTime" :label="$t('zhiFuShiJian')" min-width="120" :show-overflow-tooltip="true" />
        <el-table-column
          prop="offlineAuditTime"
          :label="$t('shenHeShiJian')"
          min-width="120"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          prop="offlineAuditAdmin"
          :label="$t('shenHeYuanXingMing')"
          min-width="120"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          prop="offlinePayFailMessage"
          :label="$t('shenPiBeiZhu')"
          min-width="120"
          :show-overflow-tooltip="true"
        />
        <el-table-column :label="$t('caoZuo')" min-width="120" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button
              v-show="scope.row.offlinePayStatus === 'doing'"
              type="text"
              size="small"
              v-hasPermi="['platform:order:offline_audit']"
              @click="openAuditDialog(scope.row)"
              >{{ $t('shenHe') }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>

    <el-dialog :title="$t('shenHe')" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item :label="$t('zhuDingDanHao')">
          <el-input v-model="ruleForm.orderNo" disabled></el-input>
        </el-form-item>
        <el-form-item :label="$t('zhuanZhangPingZheng')">
          <div class="image__preview">
            <el-image
              v-for="(item, index) in offlinePayFile"
              :key="index"
              style="width: 100px; height: 100px"
              :src="item"
              :preview-src-list="offlinePayFile"
            >
            </el-image>
          </div>
        </el-form-item>
        <el-form-item :label="$t('shenHeZhuangTai')" prop="offlinePayResult">
          <el-radio-group v-model="ruleForm.offlinePayResult">
            <el-radio label="success">{{ $t('shenHeTongGuo') }}（{{ $t('yiDaoZhang') }}）</el-radio>
            <el-radio label="fail">{{ $t('shenHeShiBai') }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          :label="$t('yuanYin')"
          prop="offlinePayFailMessage"
          v-if="ruleForm.offlinePayResult === 'fail'"
          :rules="[
            { required: ruleForm.offlinePayResult === 'fail', message: $t('qingTianXieJuJueYuanYin'), trigger: 'blur' },
          ]"
        >
          <el-input v-model="ruleForm.offlinePayFailMessage" type="textarea" :placeholder="$t('qingShuRuYuanYin')" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">{{ $t('cancel') }}</el-button>
        <el-button type="primary" @click="onSubmit">{{ $t('confirm') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { offlineOrderQueryApi, offlineAuditApi, statusNumApi } from '@/api/financial';
import * as merchant from '@/api/merchant';
import { getToken } from '@/utils/auth';
export default {
  name: 'examine',
  directives: {
    // 计算是否滚动到最下面
    selectLoadMore: {
      bind(el, binding) {
        // 获取element-ui定义好的scroll盒子
        const SELECTWRAP_DOM = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap');
        SELECTWRAP_DOM.addEventListener('scroll', function () {
          if (this.scrollHeight - this.scrollTop < this.clientHeight + 1) {
            binding.value();
          }
        });
      },
    },
  },
  data() {
    return {
      myHeaders: { 'X-Token': getToken() },
      isShow: 0,
      loadingBtn: false,
      rules: {
        offlinePayResult: [{ required: true, message: this.$t('qingXuanZeShenHeZhuangTai'), trigger: 'change' }],
        offlinePayFailMessage: [{ required: true, message: this.$t('qingTianXieJuJueYuanYin'), trigger: 'blur' }],
      },
      tableData: {
        data: [],
        total: 0,
      },
      arrivalStatusList: [
        { label: this.$t('yiDaoZhang'), value: 1 },
        { label: this.$t('weiDaoZhang'), value: 0 },
      ],
      listLoading: true,
      tableFrom: {
        dateLimit: '',
        page: 1,
        limit: 20,
        masterOrderNo: '',
        offlinePayStatus: 'doing',
        payType: 'offline',
      },
      timeVal: [],
      fromList: this.$constants.fromList,
      loading: false,
      dialogVisible: false,
      pictureVisible: false,
      transferData: {},
      merchantList: [],
      search: {
        limit: 10,
        page: 1,
        keywords: '',
      },
      ruleForm: {
        offlinePayFailMessage: '',
        offlinePayResult: '',
        orderNo: '',
      },
      localImg: '',
      offlinePayStatus: {
        need_pay: this.$t('daiZhiFu'),
        doing: this.$t('shenHeZhong'),
        pay_fail: this.$t('zhiFuShiBai'),
        has_pay: this.$t('yiZhiFu'),
      },
      chartType: {
        pay_fail: 0,
        all: 4,
        need_pay: 0,
        doing: 2,
        has_pay: 2,
      },
      offlinePayFile: [],
    };
  },
  mounted() {
    this.getList(1);
    this.getStatusNum();
  },
  methods: {
    // 获取各状态数量
    getStatusNum() {
      statusNumApi({ dateLimit: this.tableFrom.dateLimit, type: this.tableFrom.type }).then((res) => {
        this.chartType = res;
      });
    },
    openAuditDialog(row) {
      this.offlinePayFile = row.offlinePayFile;
      this.ruleForm.orderNo = row.orderNo;
      this.dialogVisible = true;
    },
    onSubmit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          offlineAuditApi(this.ruleForm).then((res) => {
            this.$message({
              type: 'success',
              message: this.$t('caoZuoChengGong'),
            });
            this.handleClose();
            this.getList('');
            this.getStatusNum();
          });
        } else {
          return false;
        }
      });
    },
    // 下拉加载更多
    selectLoadMore() {
      this.search.limit = this.search.limit + 1;
      if (this.search.limit > this.totalPage) return;
      this.getMerList(); // 请求接口
    },
    // 列表
    getMerList() {
      merchant
        .merchantListAddApi(this.search)
        .then((res) => {
          this.totalPage = res.totalPage;
          this.merchantList = this.merchantList.concat(res.list);
        })
        .catch((res) => {
          this.listLoading = false;
        });
    },
    // 选择时间
    selectChange(tab) {
      this.tableFrom.dateLimit = tab;
      this.timeVal = [];
      this.getList(1);
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      this.tableFrom.dateLimit = e ? this.timeVal.join(',') : '';
      this.getList(1);
    },
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num ? num : this.tableFrom.page;
      offlineOrderQueryApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.list.map((item) => {
            if (item.offlinePayFile) {
              item.offlinePayFile = JSON.parse(item.offlinePayFile);
            }
            return item;
          });
          this.tableData.total = res.total;
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList('');
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList('');
    },
    handleClose() {
      this.dialogVisible = false;
      this.offlinePayFile = [];
      this.ruleForm = { offlinePayFailMessage: '', offlinePayResult: '', orderNo: '' };
      this.$refs.ruleForm.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.title {
  font-size: 20px;
}
.wid100 {
  width: 100px;
}
.ml100 {
  margin-left: 100px;
}
.transferMinAmount {
  ::v-deep.el-form-item__content {
    line-height: normal;
  }
}
::v-deep .el-dialog__title {
  font-weight: bold;
}
.selWidth {
  width: 300px;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
  font-size: 12px;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.font-red {
  color: #ff4949;
}
.tabBox_tit {
  width: 60%;
  font-size: 12px !important;
  margin: 0 2px 0 10px;
  letter-spacing: 1px;
  padding: 5px 0;
  box-sizing: border-box;
}
.pictrue {
  width: 60px;
  height: 60px;
  border: 1px dotted rgba(0, 0, 0, 0.1);
  margin-right: 10px;
  position: relative;
  cursor: pointer;
  img {
    width: 100%;
    height: 100%;
  }
}
.box-container {
  overflow: hidden;
  padding: 0 30px;
  font-size: 14px;
}

.box-container .list.image {
  margin: 20px 0;
  position: relative;
}
.box-container .list.image img {
  position: absolute;
  top: -20px;
}
.box-container .list {
  float: left;
  line-height: 40px;
}
.box-container .sp100 {
  width: 100%;
}

.acea-row {
  margin-bottom: 25px;
}

.image__preview {
  .el-image + .el-image {
    margin-left: 15px;
  }
}
</style>
